#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

static void sig_handler(int sig){
    printf("执行信号处理函数...\n");
}

int main(void)
{
    struct sigaction sig = {0};
    sigset_t new_mask, old_mask, wait_mask;

    sigemptyset(&new_mask);
    sigaddset(&new_mask, SIGINT);
    sigemptyset(&wait_mask);

    sig.sa_handler = sig_handler;
    sig.sa_flags = 0;
    if(-1 == sigaction(SIGINT, &sig, NULL))
        exit(-1);

    if(-1 == sigprocmask(SIG_BLOCK, &new_mask, &old_mask))
        exit(-1);
    
    puts("执行保护代码段");

    if(-1 != sigsuspend(&wait_mask))
        exit(-1);

    if (-1 == sigprocmask(SIG_SETMASK, &old_mask, NULL))
        exit(-1);
        
    exit(0);
}